<?php


/**
 * Template for html page.
 * Initializes SESSION.
 * Prints headers, menu, content and footer of each page.
 *
 *
 * @author dll_hell_2
 */
class page {
    protected $page_title;
    public function __construct($title) 
    {
        session_start();
        $this->page_title = $title;
    }
    protected function printHeader()
    {
        return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" href="school_rs_styles.css" />
        <title>'. $this->page_title .'</title>
    </head>
    <body>';
    }
    protected function printFooter()
    {
        return '<p>This page Copyright &copy; 2011 by me! </p></body></html>';
    }
    /**
     *Prints general template of page.
     * @param string $content Html code of the page content.
     * @return string html code of the whole page.
     */
    public function __invoke($content) 
    {        
        
        $menuItems = array();
        array_push($menuItems, '<a href="index.php">Home</a>');
        if(isset($_SESSION['user']))
        {
            try
            {
                $_SESSION['user']->validate();
            }catch(Exception $e)
            {
                header('location:login.php');
            }
            array_push($menuItems, '<a href="index.php?action=logout">Logout</a>');
            array_push($menuItems, '<a href="editUser.php">Editovat uzivatele</a>');
            array_push($menuItems, '<a href="loadArticle.php">Nahrat novy clanek</a>');
            if($_SESSION['user']->getRights()>=user::$editor)
                array_push ($menuItems, '<a href="articlesManagement.php">Sprava clanku</a>');
            if($_SESSION['user']->getRights()==user::$admin)//extra menu pro admina
                    array_push ($menuItems, '<a href="usersManagement.php">Sprava uzivatelu</a>');
        }
        else        
            array_push ($menuItems, '<a href="login.php">Login</a>');
            
        
        $sections = array();
        $conn = new dbConnection();
        $query = 'SELECT * FROM sections';
        $sql_result = $conn->db_query($query);
        if(!$sql_result)
        {
            die('Invalid query: ' . mysql_error());
        }
        while($row = mysql_fetch_array($sql_result,MYSQL_ASSOC))
        {
            //podle GET nastavit barevny odliseni aktivni sekce
            array_push($sections,'<a href="index.php?section='.$row['id'].'">'.$row['name'].'</a>' );
        }
        
        $upperMenu = new menu($menuItems, 'navigation');
        $sectionMenu = new menu($sections, 'section-navigation');
        //vypsat vsechno  
        return sprintf("%s<div id=\"container\">
            <div id=\"header\">
            <h1>School_rs</h1>
            </div>%s<div id=\"content-container\">%s
            <div id=\"content\">%s</div>
            <div id=\"footer\">%s</div>
            </div>
            </div>",
                $this->printHeader(),
                $upperMenu,
                $sectionMenu,
                $content,
                $this->printFooter());
    }
}

?>
